package drawapp.cw.com.recyclerviewdemo;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private RefreshLoadingRecyclerView pullView;

    private BaseRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pullView = (RefreshLoadingRecyclerView) findViewById(R.id.pull_view);
        adapter = new BaseRecyclerAdapter(new ArrayList<String>());
        pullView.setAdapter(adapter);
        pullView.setOnRefreshLoadingListener(new RefreshLoadingRecyclerView.OnRefreshLoadingListener() {
            @Override
            public void onRefresh() {
                refresh();
            }

            @Override
            public void onLoading() {
                load();
            }
        });
        pullView.isEnableLoadMore(true);
//        pullView.setLayoutManager(new MyGridLayoutManager(adapter,this,3));
        pullView.setLayoutManager(new MyStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        pullView.setRefresh();
    }

    private void load() {
        Log.i("mydata","加载");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //模拟耗时
                    Thread.sleep(2000);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            List<String> list = new ArrayList<String>();
                            for(int i=adapter.getItemCount();i < 10 + adapter.getItemCount(); i++){
                                list.add("item--"+i);
                            }
                            Log.i("mydata","list-"+ list.get(list.size()-1));
                            adapter.addAll(list);
                            pullView.complete();
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void refresh() {
        Log.i("mydata", "refresh");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //模拟耗时
                    Thread.sleep(2000);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.clear();
                            List<String> list = new ArrayList<String>();
                            for(int i=0;i < 50; i++){
                                list.add("item--"+i);
                            }
                            adapter.addAll(list);
                            pullView.complete();
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
